package fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.projectmoudle_two.R;

/**
 * Created by eyesee on 2017/5/21.
 */

public class AnimInterplator extends BaseFragment implements View.OnKeyListener {
    private ImageView mIv;

    private static final String TAG = "eyesee";
    private static final String Anim_Type_ScaleX = "scaleX";
    private ObjectAnimator mAnimator;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mContext, R.layout.fragment_animiterplator, null);
        TextView btn = ((TextView) view.findViewById(R.id.btn));
        btn.requestFocus();
        mIv = ((ImageView) view.findViewById(R.id.iv));
        mIv.setPivotX(400);

        btn.setOnKeyListener(this);

        return view;
    }




    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN){
            if (event.getRepeatCount()==1){
                runAnimat();
                Log.i(TAG, "onKeydown=="+mAnimator.getCurrentPlayTime());
            }
        }

        if (event.getAction()==KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN){
            double playTime = mAnimator.getCurrentPlayTime();
            if ((int)playTime<=1000 && (int) playTime > 0 ){
                Log.i(TAG, "playTime111=="+(int)playTime);
                double playtimeRec = playTime / 1000;               //执行时间占总持续时间的比率
                double totalWidth = 400 * 0.3;                      //动画完全执行完成时拉伸的长度
                double width = playtimeRec * totalWidth;            //执行时间内拉伸的长度
                double rec = (width + 400) / 400;                   //view的长度是原长度的倍数
                cancleAnimat((float) rec);
            }else {
                Log.i(TAG, "playTime222=="+(int)playTime);
                cancleAnimat(1.3f);
            }
        }
        return false;
    }

    public void runAnimat() {
        mAnimator = ObjectAnimator.ofFloat(mIv, Anim_Type_ScaleX, 1, 1.3f);
        mAnimator.setDuration(1000);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.start();
    }

    public void cancleAnimat(float rec) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mIv, Anim_Type_ScaleX, rec, 1);
        animator.setDuration(1000);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

    public long getJinDu(long duration){
        long timeRec = duration / 1000;         //动画执行时间占总执行时间的比率
        double lon = 400 * 0.3;                 //动画完全执行拉伸的长度
        int width = (int) (lon * timeRec);      //动画在执行时间内拉伸的长度
        long rec = (width + 400) / 400;         //动画停止执行时的长度与原长度的比率
        return rec;
    }
}
